/**
 * Copyright (C) 2013 The yydorid-framework project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.yangying.androidframework.util;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;

import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo.State;
import android.net.Uri;
import android.webkit.URLUtil;

/**
 * NetWorkUtil
 * 
 * @author YANGYING
 * 
 * @date 2011-5-25
 */
public class NetworkUtil {

	private static final int TIMEOUT = 10 * 1000;
	
	private static NetworkUtil instance;
	private Context mContext;

	private NetworkUtil(Context paramContext) {
		Context localContext = paramContext.getApplicationContext();
		this.mContext = localContext;
	}

	public static NetworkUtil getInstance(Context paramContext) {
		synchronized (NetworkUtil.class) {
			if (instance == null) {
				instance = new NetworkUtil(paramContext);
			}
			return instance;
		}
	}

	/**
	 * 返回网络是否可用
	 * 
	 * @param context
	 * @return
	 */
	public static boolean checkNetwork(Context context) {  
        boolean flag = false;  
        ConnectivityManager cManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);  
        if (cManager.getActiveNetworkInfo() != null)  
            flag = cManager.getActiveNetworkInfo().isAvailable(); 
        return flag;
	}
	public enum NetworkState {
		NONE, MOBILE, WIFI
	}

	public  NetworkState getNetworkState() {
		ConnectivityManager conMan = (ConnectivityManager) this.mContext
				.getSystemService(Context.CONNECTIVITY_SERVICE);

		// mobile 3G Data Network
		State mobile = conMan.getNetworkInfo(ConnectivityManager.TYPE_MOBILE)
				.getState();
		// wifi
		State wifi = conMan.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
				.getState();

		if (mobile == State.CONNECTED || mobile == State.CONNECTING)
			return NetworkState.MOBILE;
		if (wifi == State.CONNECTED || wifi == State.CONNECTING)
			return NetworkState.WIFI;
		return NetworkState.NONE;

		// startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));//
		// startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
	}

	/**
	 * 获得指定url的文本内容
	 * @param url
	 * @return
	 * @throws Exception
	 */
    public static String getHttpContent(String url) throws Exception{
        StringBuilder sb = new StringBuilder();
        
        HttpClient client = new DefaultHttpClient();
        HttpParams httpParams = client.getParams();
        HttpConnectionParams.setConnectionTimeout(httpParams, 3000);
        HttpConnectionParams.setSoTimeout(httpParams, 5000);
        HttpResponse response = client.execute(new HttpGet(url));
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent(), "UTF-8"), 8192);
            
            String line = null;
            while ((line = reader.readLine())!= null){
                sb.append(line + "\n");
            }
            reader.close();
        }
        return sb.toString();
    } 
    
    @Deprecated
    public static void openUrl(Context context,String url) {
//        Uri uri = Uri.parse(url);
//        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
//        startActivity(intent);
        if(URLUtil.isNetworkUrl(url))
        {
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            intent.setClassName("com.android.browser", "com.android.browser.BrowserActivity");
            context.startActivity(intent);
        }
    }
}
